/**
 * 工作状态常量
 * @type {string}
 */
export const WORKSTATE = "workState"
/**
 * creep工作基础逻辑，没有能量时获取能量，有能量时就进行指定的工作
 * @param creep
 * @param resource_type
 */
export const workSwitch = (creep, resource_type) => {
    if (creep.memory[WORKSTATE] && creep.store[resource_type] === 0) {
        creep.memory[WORKSTATE] = false;
        // creep.say(before_messsage);
    }
    if (!creep.memory[WORKSTATE] && creep.store.getFreeCapacity() === 0) {
        creep.memory[WORKSTATE] = true;
        // creep.say(late_message);
    }
}

export const getContainerSources = (creep, sourcesIndex, road_color, freeCapacity = 1000, energySource = false) => {
    const containers = creep.room.find(FIND_STRUCTURES, {
        filter: (structure) => {
            return (structure.structureType === STRUCTURE_CONTAINER)
                && structure.store.getFreeCapacity(RESOURCE_ENERGY) < freeCapacity;
        }
    });
    if (containers.length > 0) {
        if (creep.withdraw(containers[0], RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
            creep.moveTo(containers[0], {visualizePathStyle: {stroke: road_color}})
            return true
        }
    } else {
        if (energySource) {
            getEnergySource(creep, sourcesIndex, road_color)
        }
        return false
    }
}

/**
 * 从storage中获取能量，storage中没有能量时，就去挖矿
 */
export const getStorageSources = (creep, sourcesIndex, road_color, freeCapacity = 0, energySource = false) => {
    //优先从storage中获取能量
    // const storage = creep.room.find(FIND_STRUCTURES, {
    //     filter: (i) => (i.structureType === STRUCTURE_STORAGE) &&
    //         i.store[RESOURCE_ENERGY] > freeCapacity
    // });
    const storage = creep.room.storage
    //当storage的能量不足时，通过能量矿来获取能量
    if (storage) {
        if (creep.withdraw(storage, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
            creep.moveTo(storage)
            return true
        }
    } else {
        if (energySource) {
            getEnergySource(creep, sourcesIndex, road_color)
        }
        return false
    }
}

export const getEnergySource = (creep, sourcesIndex, road_color) => {
    const source = Game.getObjectById(Memory.Rooms[creep.room.name].sources[sourcesIndex])
    // const source = creep.room.find(FIND_SOURCES)
    if (creep.harvest(source) === ERR_NOT_IN_RANGE) {
        creep.moveTo(source, {visualizePathStyle: {stroke: road_color}});
    }
}
/**
 * 获取指定类型且有存储能力的建筑
 * @param {Creep} creep
 * @param {Array} arg
 * @return {Structure[]} target
 */
export const getAppointFreeStructure = (creep, ...arg) => {
    /**
     * @type {Structure[]}
     */
    let target = [];
    for (const structureType of arg) {
        const temp = creep.room.find(FIND_MY_STRUCTURES, {
            filter: (structure) => {
                //tower能量小于900时才补充，防止一个tower能量空了后，只补充一个tower
                if (structure.structureType === STRUCTURE_TOWER) {
                    return structure.store.getFreeCapacity(RESOURCE_ENERGY) > 500;
                }
                return structure.structureType === structureType
                    && structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
            }
        })
        for (const item of temp) {
            target.push(item)
        }
    }
    if (target) {
        return target
    } else {
        console.log("指定的建筑不存在")
    }
}

/**
 * 当creep健康度小于20时回收资源，并自杀
 * @param {Creep} creep
 * @param {Structure} target
 */
export const reEnergy = (creep, target) => {
    const bodys = creep.body.map((x) => x.type)
    if (target) {
        //查看是否存在carry组件，有就回收资源，没有的话就过
        if (!creep.ticksToLive > 20 && bodys.includes(CARRY)) {
            // 转移所有资源
            for (const resourceType in creep.store) {
                if (creep.transfer(target, resourceType) === ERR_NOT_IN_RANGE) {
                    creep.moveTo(target)
                }
            }
        }
    } else {
        const storage = creep.room.storage
        if (!creep.ticksToLive > 20 && bodys.includes(CARRY)) {
            // 转移所有资源
            for (const resourceType in creep.store) {
                if (creep.transfer(storage, resourceType) === ERR_NOT_IN_RANGE) {
                    creep.moveTo(storage)
                }
            }
        }
    }

}